<?php
/*
**青岛海底世界新对接
**2015-08-29
**user by pzp
*/
class QD_underW_World{
	private $merchantCode = 'xxly.com';
	private $MerchantKey = 'F56B670B903D487584321AF0AA8EFD94'; //正式签名
	
	public function __construct(){
		$this->client =new SoapClient("http://otaapi.galasystec.net.cn/service.asmx?WSDL"); //正式地址
	}	
	
	//获取产品
	public function GetProducts(){	
		$parameters = array(
			'date'		=> '2015-09-02',
			'type'		=> '00',
			'parkCode'	=> 'hdsjjd'
		); 
		$parameters = json_encode($parameters);
		$request_array = array();
		$request_array['merchantCode'] = $this->merchantCode;
		$request_array['parameters'] = $parameters;	
		$request_array['signature']= $this->signature($parameters);
		$response = $this->client->GetProducts($request_array)->GetProductsResult;
		$response = json_decode($response,true);
		// print_r($response);exit;
		if($response['ResultCode'] == 00){
			$resultjson = json_decode($response['ResultJson'],true);
			print_r($resultjson);
		}else{
			$resultjson = json_decode($response['ResultJson'],true);
			print_r($resultjson);
		}; 
		
	}
	
	//订单回传
	public function OrderOccupies($submit_array){
		$OrderNO = array(
			'OrderNO'		=> $submit_array['OrderNO'],
			'LinkName'		=> $submit_array['LinkName'],
			'LinkPhone'		=> $submit_array['LinkPhone'],
			'LinkICNO'		=> $submit_array['LinkICNO'],
			'CreateTime'	=> date('Y-m-d H:i:s'),
		);
		$OrderNO = json_encode($OrderNO);
		$Details[] = array(
			'OrderNO'		=> $submit_array['OrderNO'],
			'ItemID'		=> $submit_array['OrderNO'],
			'ProductCode'	=> $submit_array['ProductCode'],
			'ProductID'		=> $submit_array['ProductID'],
			'ProductPackID'	=> $submit_array['ProductPackID'],
			'ProductPrice'	=> 10,
			'ProductCount'  => $submit_array['ProductCount'],
			'ProductSDate'	=> $submit_array['ProductSDate'],
			'ProductEDate'	=> $submit_array['ProductSDate']
		);
		$Details = json_encode($Details);
		$postOrder = array(
			'Ptime'			=> date('Y-m-d H:i:s'),
			'Order'			=> $OrderNO,
			'Details'		=> $Details,
			'Type'			=> '00',
			'parkCode'		=> 'hdsjjd'
		);
		$postOrder = json_encode($postOrder);
		$request_array = array(
			'merchantCode'	=> $this->merchantCode,
			'postOrder'		=> $postOrder,
			'signature'		=> $this->signature($postOrder)
		);
		// $this->logfile(json_encode($request_array),'OrderOccupies_fail');
		$response = $this->client->OrderOccupies($request_array)->OrderOccupiesResult;
		$response = json_decode($response,true);
		if($response['ResultCode'] == 00){
			$this->logfile($submit_array['OrderNO'].'|'.$response['ResultCode'],'OrderOccupies_success');
			return 100;
		}else{
			$this->logfile($submit_array['OrderNO'].'|'.$response['ResultCode'].'|'.$response['ResultMsg'],'OrderOccupies_fail');
			return 101;
		}; 
		
	}
	
	//订单完成
	public function OrderFinish($otaOrderNO){
		$parameters = array(
			'type'		=> '00',
			'parkCode'	=> 'hdsjjd'
		);
		$parameters = json_encode($parameters);
		$platformSend = 0;
		$Signature = base64_encode(strtoupper(MD5($this->merchantCode.$this->MerchantKey.$otaOrderNO.$platformSend.$parameters)));
		$request_array = array(
			'otaCode'		=> $this->merchantCode,
			'otaOrderNO'	=> $otaOrderNO,
			'platformSend'	=> $platformSend,
			'parameters'	=> $parameters,
			'signature'		=> $Signature
		);
		$response = $this->client->OrderFinish($request_array)->OrderFinishResult;
		$response = json_decode($response,true);
		if($response['ResultCode'] == 00){
			$resultjson = json_decode($response['ResultJson'],true);	
			$this->logfile($otaOrderNO.'|'.$response['ResultCode'].'|'.$resultjson[0]['ECode'],'OrderFinish_success');
			return $resultjson[0]['ECode'];		
		}else{			
			$this->logfile($otaOrderNO.'|'.$response['ResultCode'].'|'.$response['ResultMsg'],'OrderFinish_fail');
			return 101;
		}
	}
	
	//订单修改
	public function ChangeOrderEdit($UUvcode,$OrderNO){
		$Order = json_encode(array(
			'OrderNo'	=> $OrderNO
		));			
		$Details[] = array(
			'Starttime'		=> date('Y-m-d H:i:s'),
			'ProductCode'	=> $UUvcode
		);
		$Details = json_encode($Details);
		$postOrder = json_encode(array(
			'Ptime'			=> date('Y-m-d H:i:s'),
			'Order'			=> $Order,
			'Details'		=> $Details,
			'Edittype'		=> '2',
			'Type'			=> '00',
			'parkCode'		=> 'hdsjjd'
		));		
		$request_array = array(
			'merchantCode'	=> $this->merchantCode,
			'postOrder'		=> $postOrder,
			'signature'		=> $this->signature($postOrder)
		);
		$response = $this->client->ChangeOrderEdit($request_array)->ChangeOrderEditResult;
		$response = json_decode($response,true);
		if($response['ResultCode'] == 00){
			$this->logfile($OrderNO.'|'.$UUvcode.'|'.$response['ResultCode'],'ChangeOrderEdit_success');
			return 100;
		}else{
			$this->logfile($OrderNO.'|'.$UUvcode.'|'.$response['ResultCode'].'|'.$response['ResultMsg'],'ChangeOrderEdit_fail');
			return 101;
		}	
	}
	
		//获取订单状态
	public function GetAllOrderStatus($UUvcode){
		$postOrder[] = array(
			'OrderCode'		=> $UUvcode
		);
		$postOrders = json_encode($postOrder);
		$request_array = array(
			'merchantCode'	=> $this->merchantCode,
			'postOrder'		=> $postOrders,
			'signature'		=> $this->signature($postOrders),
			'BusinessType'	=> '00',
			'parkCode'		=> 'hdsjjd'
		);
		$response = $this->client->GetAllOrderStatus($request_array)->GetAllOrderStatusResult;
		$response = json_decode($response,true);
		print_r($response);exit;
		if($response['ResultCode'] == 00){
			$resultjson = json_decode($response['ResultJson'],true);
			return $resultjson;
		}else{
			$this->logfile($UUvcode.'|'.$response['ResultCode'].'|'.$response['ResultMsg'],'GetAllOrderStatus_fail');
			exit("1077|0|0|0|0|0");
		}
	}
	
	
	//计算签名
	public function signature($parameters){
		$arr = $this->merchantCode.$this->MerchantKey.$parameters;
		$signature = base64_encode(strtoupper(MD5($arr)));
		return $signature;
	}
	
	public function logfile($txt,$logname){
		$fp = fopen('/var/www/html/new/d/logs/api/QD_underW_World/'.$logname.date('Y-m-d').'.txt',"a");
		flock($fp, LOCK_EX) ;
		fwrite($fp,date('Y-m-d H:i:s').'|'.$txt."\n");
		flock($fp, LOCK_UN);
		fclose($fp); 
	} 
} 

$QD = new QD_underW_World();
// $GetProducts  = $QD->GetProducts();
// $OrderOccupies  = $QD->OrderOccupies();
// $OrderFinish  = $QD->OrderFinish("pft144117484238167461");
// $ChangeOrderEdit  = $QD->ChangeOrderEdit("pft144150419525453477"); 
$GetAllOrderStatus  = $QD->GetAllOrderStatus("504856316016"); 
?>